home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 04 Pathfinding and Movement / 05 Hancock / Goal_GotoNode.cpp < prev    next >
Encoding:
Text File  |  2001-08-21  |  1.7 KB  |  84 lines

  1.  
  2.  
  3. Goal_GotoNode::Goal_GotoNode( AI* pAI, const PathNode *destination, 
  4.                                               bool bForceDirectPath /*= false*/)
  5.                                               : Goal( pAI ), GoalQueue(), node(destination),
  6.                                               active(false), directPath(bForceDirectPath)
  7.                                                 
  8. {
  9. }
  10.  
  11. Goal_GotoNode::~Goal_GotoNode()
  12. {
  13. }
  14.  
  15. bool Goal_GotoNode::ReplanSubgoals(){
  16.     ResetSubgoals();
  17.  
  18.     stuckTimer = 0.0f;
  19.  
  20.     if (directPath){ //don't plan a path if we've been told to make a beeline for the goal
  21.         return true;
  22.     }
  23.     
  24.     //find the closest node to our current position
  25.     const PathNode *nodeStart = g_NodeMap.FindClosestVisibleNode(mpAI->WorldPos());
  26.  
  27.  
  28.     if (!nodeStart){
  29.         SetStatus( statusFailed );
  30.         return false; //we couldn't find a node near us, abandon all hope!
  31.     }
  32.     
  33.     float pathLength = mpAI->GeneratePathingGoals(nodeStart, node, *this);
  34.     if (pathLength < 0){
  35.         SetStatus( statusFailed );
  36.         return false;
  37.     }
  38.  
  39.     return true;
  40. }
  41.  
  42.  
  43. bool Goal_GotoNode::Success() {    
  44.    return (mpAI->WorldPos().distance(node->pos) < node->radius);
  45. }
  46.  
  47.  
  48. // Update the goal
  49. void Goal_GotoNode::Update( float secs_elapsed )
  50. {    
  51.     typeGoalStatus status = UpdateSubgoals( secs_elapsed );
  52.     
  53.     if (status==statusFailed) {
  54.         SetStatus( statusFailed );    
  55.         return; 
  56.     }
  57.     
  58.     if (!NoSubgoals()) return;
  59.     
  60.     if (Success()) {
  61.         SetStatus( statusSucceeded );
  62.         return; 
  63.     }
  64.  
  65.     if (!active){ //if we weren't previously active, we should now generate a plan to goto the node
  66.         ReplanSubgoals();
  67.         active = true;
  68.         return;
  69.     }
  70.     
  71.     mpAI->Crouch(node->flags & PathNode::flagNodeCrouch);
  72.     
  73.     mpAI->Servo(node->pos);
  74.     
  75.     if (mpAI->IsStuck())
  76.         stuckTimer += secs_elapsed;
  77.     
  78.     //check if we are stuck or not
  79.     if (stuckTimer > 1.0) {
  80.         SetStatus( statusFailed ); /*ReplanSubgoals();*/
  81.         return; 
  82.     }
  83. }
  84.